//
//  LEMyOrderController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/12.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEMyOrderController.h"
#import "MakerTools.h"
#import "LEStoreModel.h"

#import "LEOrderCenterController.h"

@interface LEMyOrderController ()

@property (weak, nonatomic) IBOutlet UILabel *todayOrderLabel;
@property (weak, nonatomic) IBOutlet UILabel *historyOrderLabel;
@property (weak, nonatomic) IBOutlet UISwitch *orderSwitch;

@end

@implementation LEMyOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    LEStoreModel *store = [MakerTools readFromSandBox:Store_Info];
    if ([store.store_orders_delivery isEqualToString:@"1"])
    {
        [self.orderSwitch setOn:YES];
    }else{
        [self.orderSwitch setOn:NO];
    }
    
}
- (void)loadData
{
    [self requestMyOrder];
}
#pragma mark- 请求数据
- (void)requestMyOrder
{
    
    [API requestAppendKey:StoreMyordereUrl params:nil success:^(NSDictionary *successCode) {
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        if (error) {
            [SVProgressHUD showErrorWithStatus:error];
        }else{
            self.todayOrderLabel.text = datas[@"daily"];
            self.historyOrderLabel.text = datas[@"historyly"];
        }
    } falure:^(NSError *er) {
        
    }];
}

/**
 自动派单
 */
- (void)requestAutoSendOrder:(NSDictionary *)param
{
    [API requestPostAppendKey:StoreAutoSendOrderUrl params:param success:^(NSDictionary *successCode) {
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
        }
    } falure:^(NSError *er) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
    
}
#pragma mark- 点击事件
- (IBAction)todayClick:(UIButton *)sender {
    LELog(@"%s---%d---今日订单",__func__,__LINE__);
    [self pushOrderCenter:TodayOrder];
}
- (IBAction)historyClick:(UIButton *)sender {
    LELog(@"%s---%d---历史订单",__func__,__LINE__);
    [self pushOrderCenter:HistoryOrder];
}
- (IBAction)autoSendOrder:(UISwitch *)sender {
    LELog(@"%s---%d---%d",__func__,__LINE__,sender.on);
    LEStoreModel *store = [MakerTools readFromSandBox:Store_Info];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (sender.on) {
        store.store_orders_delivery = @"1";
        [param setObject:@"store_orders_delivery|1" forKey:@"param"];
        
    }else{
        store.store_orders_delivery = @"2";
        [param setObject:@"store_orders_delivery|2" forKey:@"param"];
    }
    [MakerTools saveToSandBox:Store_Info withSavedObject:store];
    [self requestAutoSendOrder:param];
}

#pragma mark- privateMethod

/**
 跳转到订单中心

 @param mode 订单中心的类型 :今日订单,历史订单
 */
- (void)pushOrderCenter:(OrderMode)mode
{
    LEOrderCenterController *orderCenter = [[LEOrderCenterController alloc] init];
    orderCenter.orderMode = mode;
    orderCenter.menuViewStyle = WMMenuViewStyleLine;
    orderCenter.selectIndex = 0;
    orderCenter.menuHeight = 40;
    orderCenter.titleColorSelected = LESystemBlueColor;
    orderCenter.titleColorNormal = LEHaxColor(0x707070);
    orderCenter.progressWidth = LEScreenWidth /3.0;
    orderCenter.menuBGColor = [UIColor whiteColor];
    orderCenter.titleSizeSelected = 15;
    [self.navigationController pushViewController:orderCenter animated:YES];
}

@end





